% Generator: GNU source-highlight, by Lorenzo Bettini, http://www.gnu.org/software/src-highlite

{\ttfamily \raggedright {
\noindent
\mbox{}\textbf{\textcolor{Blue}{struct}}\ point\textcolor{Red}{\{} \\
\mbox{}\ \ \textcolor{ForestGreen}{double}\ x\textcolor{BrickRed}{,}y\textcolor{BrickRed}{;} \\
\mbox{}\textcolor{Red}{\}}\textcolor{BrickRed}{;} \\
\mbox{} \\
\mbox{}\textbf{\textcolor{Blue}{inline}}\ \textcolor{ForestGreen}{double}\ \textbf{\textcolor{Black}{dist}}\textcolor{BrickRed}{(}\textbf{\textcolor{Blue}{const}}\ point\ \textcolor{BrickRed}{\&}a\textcolor{BrickRed}{,}\ \textbf{\textcolor{Blue}{const}}\ point\ \textcolor{BrickRed}{\&}b\textcolor{BrickRed}{)}\textcolor{Red}{\{} \\
\mbox{}\ \ \textbf{\textcolor{Blue}{return}}\ \textbf{\textcolor{Black}{sqrt}}\textcolor{BrickRed}{((}a\textcolor{BrickRed}{.}x\textcolor{BrickRed}{-}b\textcolor{BrickRed}{.}x\textcolor{BrickRed}{)*(}a\textcolor{BrickRed}{.}x\textcolor{BrickRed}{-}b\textcolor{BrickRed}{.}x\textcolor{BrickRed}{)}\ \textcolor{BrickRed}{+}\ \textcolor{BrickRed}{(}a\textcolor{BrickRed}{.}y\textcolor{BrickRed}{-}b\textcolor{BrickRed}{.}y\textcolor{BrickRed}{)*(}a\textcolor{BrickRed}{.}y\textcolor{BrickRed}{-}b\textcolor{BrickRed}{.}y\textcolor{BrickRed}{));} \\
\mbox{}\textcolor{Red}{\}} \\
\mbox{} \\
\mbox{}\textbf{\textcolor{Blue}{inline}}\ \textcolor{ForestGreen}{double}\ \textbf{\textcolor{Black}{distsqr}}\textcolor{BrickRed}{(}\textbf{\textcolor{Blue}{const}}\ point\ \textcolor{BrickRed}{\&}a\textcolor{BrickRed}{,}\ \textbf{\textcolor{Blue}{const}}\ point\ \textcolor{BrickRed}{\&}b\textcolor{BrickRed}{)}\textcolor{Red}{\{} \\
\mbox{}\ \ \textbf{\textcolor{Blue}{return}}\ \textcolor{BrickRed}{(}a\textcolor{BrickRed}{.}x\textcolor{BrickRed}{-}b\textcolor{BrickRed}{.}x\textcolor{BrickRed}{)*(}a\textcolor{BrickRed}{.}x\textcolor{BrickRed}{-}b\textcolor{BrickRed}{.}x\textcolor{BrickRed}{)}\ \textcolor{BrickRed}{+}\ \textcolor{BrickRed}{(}a\textcolor{BrickRed}{.}y\textcolor{BrickRed}{-}b\textcolor{BrickRed}{.}y\textcolor{BrickRed}{)*(}a\textcolor{BrickRed}{.}y\textcolor{BrickRed}{-}b\textcolor{BrickRed}{.}y\textcolor{BrickRed}{);} \\
\mbox{}\textcolor{Red}{\}} \\
\mbox{} \\
\mbox{}\textit{\textcolor{Brown}{/*}} \\
\mbox{}\textit{\textcolor{Brown}{\ \ Returns\ the\ closest\ distance\ between\ point\ pnt\ and\ the\ segment\ that\ goes\ from\ point\ a\ to\ b}} \\
\mbox{}\textit{\textcolor{Brown}{\ \ Idea\ by:\ }}\underline{\texttt{\textcolor{Blue}{http://local.wasp.uwa.edu.au/}}}\textit{\textcolor{Brown}{\textasciitilde{}pbourke/geometry/pointline/}} \\
\mbox{}\textit{\textcolor{Brown}{\ */}} \\
\mbox{}\textcolor{ForestGreen}{double}\ \textbf{\textcolor{Black}{distance$\_$point$\_$to$\_$segment}}\textcolor{BrickRed}{(}\textbf{\textcolor{Blue}{const}}\ point\ \textcolor{BrickRed}{\&}a\textcolor{BrickRed}{,}\ \textbf{\textcolor{Blue}{const}}\ point\ \textcolor{BrickRed}{\&}b\textcolor{BrickRed}{,}\ \textbf{\textcolor{Blue}{const}}\ point\ \textcolor{BrickRed}{\&}pnt\textcolor{BrickRed}{)}\textcolor{Red}{\{} \\
\mbox{}\ \ \textcolor{ForestGreen}{double}\ u\ \textcolor{BrickRed}{=}\ \textcolor{BrickRed}{((}pnt\textcolor{BrickRed}{.}x\ \textcolor{BrickRed}{-}\ a\textcolor{BrickRed}{.}x\textcolor{BrickRed}{)*(}b\textcolor{BrickRed}{.}x\ \textcolor{BrickRed}{-}\ a\textcolor{BrickRed}{.}x\textcolor{BrickRed}{)}\ \textcolor{BrickRed}{+}\ \textcolor{BrickRed}{(}pnt\textcolor{BrickRed}{.}y\ \textcolor{BrickRed}{-}\ a\textcolor{BrickRed}{.}y\textcolor{BrickRed}{)*(}b\textcolor{BrickRed}{.}y\ \textcolor{BrickRed}{-}\ a\textcolor{BrickRed}{.}y\textcolor{BrickRed}{))}\ \textcolor{BrickRed}{/}\ \textbf{\textcolor{Black}{distsqr}}\textcolor{BrickRed}{(}a\textcolor{BrickRed}{,}\ b\textcolor{BrickRed}{);} \\
\mbox{}\ \ point\ intersection\textcolor{BrickRed}{;} \\
\mbox{}\ \ intersection\textcolor{BrickRed}{.}x\ \textcolor{BrickRed}{=}\ a\textcolor{BrickRed}{.}x\ \textcolor{BrickRed}{+}\ u\textcolor{BrickRed}{*(}b\textcolor{BrickRed}{.}x\ \textcolor{BrickRed}{-}\ a\textcolor{BrickRed}{.}x\textcolor{BrickRed}{);} \\
\mbox{}\ \ intersection\textcolor{BrickRed}{.}y\ \textcolor{BrickRed}{=}\ a\textcolor{BrickRed}{.}y\ \textcolor{BrickRed}{+}\ u\textcolor{BrickRed}{*(}b\textcolor{BrickRed}{.}y\ \textcolor{BrickRed}{-}\ a\textcolor{BrickRed}{.}y\textcolor{BrickRed}{);} \\
\mbox{}\ \ \textbf{\textcolor{Blue}{if}}\ \textcolor{BrickRed}{(}u\ \textcolor{BrickRed}{$<$}\ \textcolor{Purple}{0.0}\ \textcolor{BrickRed}{$|$$|$}\ u\ \textcolor{BrickRed}{$>$}\ \textcolor{Purple}{1.0}\textcolor{BrickRed}{)}\textcolor{Red}{\{} \\
\mbox{}\ \ \ \ \textbf{\textcolor{Blue}{return}}\ \textbf{\textcolor{Black}{min}}\textcolor{BrickRed}{(}\textbf{\textcolor{Black}{dist}}\textcolor{BrickRed}{(}a\textcolor{BrickRed}{,}\ pnt\textcolor{BrickRed}{),}\ \textbf{\textcolor{Black}{dist}}\textcolor{BrickRed}{(}b\textcolor{BrickRed}{,}\ pnt\textcolor{BrickRed}{));} \\
\mbox{}\ \ \textcolor{Red}{\}} \\
\mbox{}\ \ \textbf{\textcolor{Blue}{return}}\ \textbf{\textcolor{Black}{dist}}\textcolor{BrickRed}{(}pnt\textcolor{BrickRed}{,}\ intersection\textcolor{BrickRed}{);} \\
\mbox{}\textcolor{Red}{\}} \\

} \normalfont\normalsize
